package com.atguigu.Flink.state;

import com.atguigu.Flink.POJO.WindSpeedSensor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Flink05_KeyedValueState {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //需求：检测每种传感器的风速，如果连续的两个风速差值为10，就输出报警
        env.socketTextStream("hadoop102",8888)
                .map(
                        line->{
                            String[] split = line.split(",");
                            return new WindSpeedSensor(split[0],Integer.valueOf(split[1]),Long.valueOf(split[2]));
                        }
                ).keyBy(
                        k->k.getId()
                ).process(
                        new KeyedProcessFunction<String, WindSpeedSensor, String>() {
                            //声明值状态，用于维护上次的风速
                            private ValueState<Integer> valueState;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                //对值状态进行初始化
                                ValueStateDescriptor<Integer> valueStateDescriptor = new ValueStateDescriptor<>("valueState", Types.INT);
                                 valueState = getRuntimeContext().getState(valueStateDescriptor);
                            }

                            @Override
                            public void processElement(WindSpeedSensor windSpeedSensor, KeyedProcessFunction<String, WindSpeedSensor, String>.Context context, Collector<String> collector) throws Exception {
                                Integer upspeed = valueState.value();
                                if (upspeed == null){
                                    valueState.update(windSpeedSensor.getSpeed());
                                    collector.collect("本次风速" + windSpeedSensor.getSpeed().toString());
                                }else {
                                    if(windSpeedSensor.getSpeed() - upspeed >= 10){
                                        collector.collect("危险，两次风速差值达到阈值，差值为：" + (windSpeedSensor.getSpeed() - upspeed) );
                                    }else {
                                        collector.collect("安全，本次风速为：" + windSpeedSensor.getSpeed());
                                    }
                                }

                            }
                        }
                ).print();



        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
